package com.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.app.R;
import com.app.constant.Constants;
import com.app.http.HttpDataLoader;
import com.app.http.HttpDataReqUrl;
import com.app.model.request.ReqObj;
import com.app.model.response.GetWalletRes;
import com.app.model.response.ResObj;
import com.app.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ActivityWallet extends ActivityBaseMenuText {

    private static final String LOG_TAG = ActivityWallet.class.getSimpleName();

    @BindView(R.id.tvBlance)
    TextView tvBlance;

    private HttpDataLoader dataLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet);
        ButterKnife.bind(this);
        setTitle("我的钱包");
        dataLoader = new HttpDataLoader(this);
        setMenuText("明细");
        setOnMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ActivityWallet.this, ActivityWalletDetailList.class));
            }
        });
        getWallet();
    }

    private void getWallet() {
        ReqObj req = new ReqObj();
        dataLoader.POST(HttpDataReqUrl.URL_GET_WALLET, req);
    }

//    @OnClick({R.id.tvCharge})
//    public void onClick(View view) {
//        Intent intent;
//        switch (view.getId()) {
//            case R.id.tvCharge:
//                intent = new Intent(ActivityWallet.this, ActivityCharge.class);
//                startActivity(intent);
//                break;
//            case R.id.tvWithdraw:
//                intent = new Intent(ActivityWallet.this, ActivityWithdraw.class);
//                startActivity(intent);
//                break;
//            default:
//                break;
//        }
//    }

    @Override
    public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
        Log.e(LOG_TAG, "onRequestSuccess");
        Log.e(LOG_TAG, "url: " + url);
        Log.e(LOG_TAG, "result: " + result);
        if (url.equals(HttpDataReqUrl.URL_GET_WALLET)) {
            if (result.getCode() == Constants.CODE_SUCCESS) {
                if (result.getData() instanceof GetWalletRes) {
                    GetWalletRes getWalletRes = (GetWalletRes) (result.getData());
                    tvBlance.setText(String.valueOf(getWalletRes.getBalance()));
                } else {
                    ToastUtil.getInstance().showToast(this, result.getMsg());
                }
            } else {
                ToastUtil.getInstance().showToast(this, result.getMsg());
            }
        }
    }

    @Override
    public void onRequestError(String url, int statusCode, String error) {
        Log.e(LOG_TAG, "onRequestError");
        Log.e(LOG_TAG, "url: " + url);
        Log.e(LOG_TAG, "statusCode: " + statusCode);
        ToastUtil.getInstance().showToast(this, error);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getWallet();
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(0, R.anim.push_left_out);
    }
}
